import re
import sys
import os
import time

BASE_get_data = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0,BASE_get_data)

import pandas as pd
import json
import numpy as np
import requests_get
import com
import dfmethod

pd.set_option('display.max_rows', None)
pd.set_option('display.width', 5000)
def read_250day_json():
    path_db = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
    path = os.path.join(path_db, "db", "tushare_his", "250day", '{}.json'.format(com.time_saveandget()))

    df=pd.read_csv(path, index_col= 0)
    df=df.dropna()
    df["wr"]=(df.close-df["250日"])/df.close
    return df
class BK_analyse:
    """
    BK_analyse()._zhangfu_main(1)#1代表今日，2代表昨天，3代表前天
    BK_analyse().zhangfu_main(10)#个股涨幅

    BK_analyse().zhangting(name="longhu",time_day="20210930")
    BK_analyse().zhangting(name="zhangting", time_day="20210930")
    """
    print("dxw分析开始")
    def __init__(self):
        self.path_db= os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
        self.path={"gegu":os.path.join(self.path_db, "db", "dxw", "gegu", '{}.json'),
                   "gegu_zhangfu_bankuai": os.path.join(self.path_db, "db", "dxw", "gegu","bankuai", '{}.json'),
                   "gegu_zhangfu_bankuai_choose": os.path.join(self.path_db, "db", "dxw", "gegu", "bankuai", '{}_choose.json'),
                   "zhangting":os.path.join(self.path_db, "db", "dxw", "zhangting", '{}.json'),
                   "zhangting_ansys_new": os.path.join(self.path_db, "db", "dxw", "zhangting", "ansys", '{}.json'),
                   "zhangting_ansys_old": os.path.join(self.path_db, "db", "dxw", "zhangting", "ansys",'{}_{}.json'),
                   "longhu": os.path.join(self.path_db, "db", "dxw", "longhu", '{}.json'),
                   "ansys": os.path.join(self.path_db, "db", "dxw", "ansys", '{}.json'),
                  }

        pass
    # ————————————————————————————————————————————————————————————
    #1获取数据
    def read_dxw_gegu_json(self,*args,**kwargs):
        global time_day
        time_day=com.time_saveandget(choose="day_list",day=25)[-args[0]]
        #print("->获取数据的time：",time_day)
        path =self.path["gegu"].format(time_day)
        if os.path.exists(path) is False:
            try:
                requests_get.dxw._gegu_except(day="{}".format(time_day))
                print("成功获得{}数据".format(time_day))
            except:
                print("获取数据失败，----->try的方法不对，还需完善")
                return False,"没获得数据,数据为None,请获得{}数据,运行get_data/request_get/".format(time_day),None
        df=pd.read_csv(path, index_col= 0)

        df=dfmethod.gegu_method1(df)
        return True,"成功获取数据",df
    #2数据加工
    """->板块热门    板块   主力资金  股票    板块code       day
        0    热门   锂电池  58.67  49  GN300118  20210914
        1    其他   金属锂  10.21   2  GN302464  20210914
        2    热门  稀土永磁   9.52  11  GN300171  20210914
        3    热门   有机硅   8.06   5  GN302284  20210914
    """
    def _zhangfu_main(self,day):
        flag,msg,df_return = self.read_dxw_gegu_json(day)
        if flag==True:
            df_return=dfmethod.gegu_method2(df_return)
            df_return["day"]=time_day
            return df_return
        else:
            print("False,BK_analyse.read_dxw_gegu_json失败")
            return df_return
    # 3.for循环+主函数

    def zhangfu_main(self, day_num="7"):
        # 1.
        df_return = pd.DataFrame()
        # debug
        # print(com.time_start()[1]==com.time_saveandget())
        if com.time_passed_9_30() <= 0 and (com.time_start()[1] == com.time_saveandget()):
            range_begin = 2
        else:
            range_begin = 1
        # ->
        """    板块热门    板块   主力资金  股票    板块code       day
            0     热门   锂电池  31.10  39  GN300118  20210930
            1     其他  电气设备  11.21  33  GN302050  20210930
            2     热门    电力   8.72  16  GN302950  20210930
        """
        for i in range(range_begin, range_begin + day_num):
            df = self._zhangfu_main(i)
            df_return = df_return.append(df, ignore_index=True)
        df_return = dfmethod.gegu_method4(df_return)
        # ->保存
        """
        整合时间pd.pivot_table                  主力资金  股票       day
            板块   板块code   number 板块热门                     
            光伏   GN300031 8      其他    16.14  12  20210917
                                 其他    14.53  18  20210922
                                 其他     5.02   7  20210923
                                 其他    34.42  32  20210924
                                 其他    16.78  14  20210927
                                 其他     9.94  15  20210928
                                 其他    13.18  19  20210929
                                 其他     3.66  13  20210930
            白酒   GN300200 7      热门    46.94  13  20210917
        """
        df_return = df_return.reset_index(drop=True)
        time_ = com.time_saveandget(choose="day_list", )[-range_begin]
        path_ = self.path["gegu_zhangfu_bankuai"].format(time_)
        df_return.to_csv(path_, encoding="utf-8")
        print("保存到dxw/gegu/bankuai/{}.json中".format(time_))

        # 2.计算每个板块每天的涨幅。思路：for循环打开每个文件，并进行统计。
        """保存到dxw/gegu/bankuai/.choose20211011中     板块热门    板块   主力资金  股票    板块code       day  number  当日涨停
0     其他    光伏  16.14  12  GN300031  20210917       8     3
1     其他    光伏  14.53  18  GN300031  20210922       8     6
2     其他    光伏   5.02   7  GN300031  20210923       8     1
3     其他    光伏  34.42  32  GN300031  20210924       8     5
4     其他    光伏  16.78  14  GN300031  20210927       8     1
5     其他    光伏   9.94  15  GN300031  20210928       8     2
6     其他    光伏  13.18  19  GN300031  20210929       8     2
7     其他    光伏   3.66  13  GN300031  20210930       8     2
8     热门    白酒  46.94  13  GN300200  20210917       7     2
9     其他    白酒   6.47   3  GN300200  20210923       7     0
10    热门    白酒  44.94  14  GN300200  20210924       7     0
        """
        list_ = []
        for i in range(len(df_return)):
            day_i = df_return.loc[[i], ["day"]].values[0][0]
            bk_code = df_return.loc[[i], ["板块code"]].values[0][0]
            """
            ::::::::::: 20210917 GN300031 光伏
            ::::::::::: 20210922 GN300031 光伏
            ::::::::::: 20210923 GN300031 光伏
            """
            # print( ":::::::::::",day_i,bk_code,df_return.loc[[i],["板块"]].values[0][0])
            # 个股位置
            # print(self.path["gegu"].format(day_i))
            df_ = pd.read_csv(self.path["gegu"].format(day_i), index_col=0)
            # 当日涨幅＞9.9的板块个数
            # print(dfmethod.df_methodPass10(day_i, bk_code,self.path["gegu"].format(day_i)))
            list_.append(dfmethod.df_methodPass10(day_i, bk_code, self.path["gegu"].format(day_i)))
        df_return["当日涨停"] = list_

        # print(df_[df_["板块code"]==bk_code])
        path_ = self.path["gegu_zhangfu_bankuai_choose"].format(time_)
        df_return.to_csv(path_, encoding="utf-8")
        print("保存到dxw/gegu/bankuai/{}_choose.json中".format(time_), df_return)
        return df_return

    def longhuansys(self,time_day):
        path =self.path["longhu"].format(time_day)
        if os.path.exists(path) is False:
            try:
                requests_get.dxw.longhu(day="{}".format(time_day))
                print("成功获得{}数据".format(time_day))
            except:
                print("获取龙虎榜失败")
        df=pd.read_csv(path,index_col= 0)
        df=dfmethod.gegu_method5(df)


    # ————————————————————————————————————————————————————————————
    #todo#资金分析
    @property
    def zijin_df(self):
        flag,msg,df= self.read_dxw_gegu_json()
        df=df.sort_values(by=['主动净买'], ascending=False)
        self.zhangfu_list.append(df)

    #3.获取数据主函数+微加工
    def zhangting(self,name,time_day):
        if time_day == "":
            time_day = com.time_saveandget()
        else:
            time_day = time_day
        path =self.path["{}".format(name)].format(time_day)
        if os.path.exists(path) is False:
            print("->文件不存在")
            requests_get.dxw.daban(name="zhangting",day=time_day)
        df = pd.read_csv(path, index_col= 0)
        print("->读取{}成功".format(path))
        return df
    def zhangting_main(self,day,method=None):
        self.day=day
        zhangting_df=self.zhangting(name="zhangting", time_day=day)

        yuanzu1=dfmethod.gegu_method3(zhangting_df)
        #todo
        if method=="history":
            print("gothisway")
        #todo
        path =self.path["ansys"].format(day)
        df_=self.bkforgncode(yuanzu1)

        print("保存到dxw/ansys/{}中".format(day))
        df_.to_csv("{}".format(path), encoding="utf-8")
    def bkforgncode(self,yuanzu):
        df_, list_bk, list_bk_code=yuanzu
        print(list_bk, list_bk_code)
        #板块计数
        df_bk_count = df_.板块.value_counts()
        print(df_bk_count[df_bk_count >= 2])
        i_count = 0
        df_return = pd.DataFrame()
        for i in list_bk:
            print(df_[df_['板块'] == i])
            print(":::::::::::::::::::::::::::::::::::")
            print(list_bk[i_count],self.day)
            gn_df=requests_get.dxw.gn_bkcode(gncode=list_bk_code[i_count],day=self.day)
            df_return = df_return.append(gn_df, ignore_index=True)
            print(gn_df)
            i_count+=1
        return df_return

    def different(self,day):
        zhangting_df_new= requests_get.dxw.daban(name="zhangting",day=day)
        df_=dfmethod.gegu_method3(zhangting_df_new,method="1")

        df_new_path = self.path["zhangting_ansys_new"].format(day)
        df_.to_csv("{}".format(df_new_path), encoding="utf-8")
        # print(df_)
        df_old_path=self.path["zhangting_ansys_old"].format(day,"old")

        def have_true():#读取、差异化、替换
            df_old = pd.read_csv(df_old_path)
            # 交集操作a1是新，a2是Ⅸ
            a1 = df_.股票.tolist()
            a2 = df_old.股票.tolist()
            print('交集', [j for j in a1 if j in a2])
            print("新进名单", list(set(a1).difference(set(a2))))  # b中有而a中没有的
            print("新剔除名单", list(set(a2).difference(set(a1))))
            df_.to_csv("{}".format(df_old_path), encoding="utf-8")
        if os.path.exists(df_old_path) is True:#print("存在old")
            have_true()
        else:#不存在old/创建old/保存df old/重新运行函数
            df_.to_csv("{}".format(df_old_path), encoding="utf-8")
            # self.different(day)
            have_true()

        '''
        里面是否有old
        1.you old
        2.没有old
        
        '''

        # zhangting_df_old
        # if os.path.exists(path) is False:
        #     print("不存在")
        #     df_ = self.bkforgncode(yuanzu1)
        # read_ansys=pd.read_csv(path)
        # print("diff",read_ansys)


if __name__ == '__main__':
    BK_analyse().longhuansys('20211019')
    # a = BK_analyse()._zhangfu_main(11)
    a=BK_analyse().zhangfu_main(10)#个股涨幅
    # BK_analyse().zhangting_main(day="20210930")#涨停分析
    #BK_analyse().different(day="20210929")#涨停差异
    # BK_analyse.read_dxw_gegu_json()
    # print(requests_get.choose("同花顺top100"))



    import requests
    import simplejson
    def dxw_api_zhangfu_gegu(code_="GN300200", daytime=com.time_saveandget()):
        url = 'http://api.quchaogu.com/event/hangye/stockfordxw?start_time=930&pagecount=20&last_time=1500&end_time=1500&filter_date={}&page=1&code={}&apiversion=8.7&backgroundcolor=white&vaid=&oaid=&device_id=a865166028641465'.format(
            daytime, code_)
        headers = {
            'User-Agent': 'DxwApp:5.3.0.200 | android:5.1.1 | samsung:SM-G9730 | sc:360,640 | did:a865166028641465 | oaid: | vaid: | did:a865166028641465 | idfa: | qd:wap | av:8.7 | uid:7%253A8%253A2206148315%253Aa865166028641465%253A6%253A1605528165%253A289aadf8d7a35a1',
            'Host': 'api.quchaogu.com',
            'Connection': 'Keep-Alive',
            'Accept-Encoding': 'gzip',
            'Cookie': 'web_uinfo=%7B%22uactime%22%3A1605530211%7D; app_ua=DxwApp%3A4.2.0.156+%7C+android%3A5.1.1+%7C+samsung%3ASM-G9730+%7C+sc%3A360%2C640+%7C+did%3Aa865166028641465+%7C+oaid%3A+%7C+vaid%3A+%7C+did%3Aa865166028641465+%7C+idfa%3A+%7C+qd%3Awap+%7C+av%3A8.7+%7C+uid%3A7%25253A8%25253A2206148315%25253Aa865166028641465%25253A6%25253A1605528165%25253A289aadf8d7a35a1; vistor_uuid=32717565199866433743603; web_qtstr=7%3A8%3A2206148315%3Aa865166028641465%3A6%3A1605528165%3A289aadf8d7a35a1'
        }
        params = {
            'start_time': '930',
            'last_time': '1500',
            'end_time': '1018',
            'filter_date': '{}'.format(daytime),
            'pagecount': '20',
            'page': '1',
            'code': '{}'.format(code_),
            'apiversion': '8.7',
            'backgroundcolor': 'white',
            'vaid': '',
            'oaid': '',
            'device_id': 'a865166028641465'}

        r = requests.get(url, headers=headers, params=params)
        if r.status_code == 200:
            r = r.text.encode('utf-8').decode('unicode_escape')
            print(simplejson.loads(r, strict=False)['data']['day'])  # 20210330
            a = simplejson.loads(r, strict=False)['data']['stock_list']
            df = pd.DataFrame(a)

            """text         16 non-null object
        color        16 non-null int64
        icons        16 non-null object   *****
        type         16 non-null object
        fixed_ext    16 non-null object
        remark       16 non-null object
        param        16 non-null object   *****
        bold         16 non-null int64
        next_tags    16 non-null object   *****
        is_new       16 non-null int64    *****
        is_yd        1 non-null float64   *****"""
            # is_new  is_yd (异动)
            today_time_save = com.time_saveandget()
            try:
                result_ = df[0].apply(pd.Series)[["icons", "param", "next_tags", "is_new", "is_yd"]]
                print(pd.DataFrame(result_))
            except:
                result_ = df[0].apply(pd.Series)[
                    ["icons", "param", "next_tags", "is_new"]]  # [["icons","param","next_tags","is_new","is_yd"]]
                # print(pd.DataFrame(result_))
                result_ = pd.DataFrame({"param": pd.DataFrame(result_.param.values.tolist())['code'],
                                        'icons': np.concatenate(result_.icons.values), "next_tags": result_.next_tags,
                                        "is_new": result_.is_new})  # dataframe里面列的数据类型是list的列拆分成多个行
                # print(result_)
                # print(result_.icons.str.split("/").apply(pd.Series)[5].str.split(".").apply(pd.Series)[0])

            a_list = []
            for i in result_.next_tags:
                # print(i)#['http://idxw.oss-cn-beijing.aliyuncs.com/icon/tag/renqilong1.png']
                # print(str(i).split("/")[0],len(str(i).split("/")[0])) #['http: 7
                if len(str(i).split("/")[0]) == 7:
                    # print(str(i).split("/")[5].split(".")[0]) #renqilong4
                    a_list.append(str(i).split("/")[5].split(".")[0])
                else:
                    # print("")
                    a_list.append("")
            # a_list
            result_["renqi_next_tags"] = a_list
            result_["hangye_icons"] = result_.icons.str.split("/").apply(pd.Series)[5].str.split(".").apply(pd.Series)[
                0]
            # print(result_[["param","is_new","renqi_next_tags","hangye_icons"]])
            result_ = pd.concat(
                [result_[["param", "is_new", "renqi_next_tags", "hangye_icons"]], df[1].apply(pd.Series).text,
                 df[2].apply(pd.Series).text, df[3].apply(pd.Series).text, df[4].apply(pd.Series).text], axis=1)
            # print(df[1].apply(pd.Series).text,df[2].apply(pd.Series).text,df[3].apply(pd.Series).text,df[4].apply(pd.Series).text)

            result_.columns = ["param", "is_new", "renqi_next_tags", "hangye_icons", "涨幅", "30主动", "主净买", "主资金"]
            result_["hangye_icons"] = result_.hangye_icons.str.replace('bk_tag_high', '').str.replace('bk_tag_mid',
                                                                                                      '').str.replace(
                'bk_tag_low', '')
            print(result_)

            return result_


    """
    a=shiyan.dxw_api_zhangfu_gegu(code_="GN300036",daytime="20210330")
    a.columns=["param","is_new","renqi_next_tags","hangye_icons","涨幅","30主动","主净买","主资金"]
    a.hangye_icons.str.replace('bk_tag_high','').str.replace('bk_tag_mid','').str.replace('bk_tag_low','')
    """
    """20210330
         param  is_new renqi_next_tags hangye_icons        涨幅     30主动     主净买  \
    0   300159       0      renqilong1                +19.88%    -865万  -3452万   
    1   600760       0      renqilong3  hangyelong2   +10.00%   -3715万  -1427万   
    2   600316       0                                 +6.65%   -1131万   8187万   
    3   300424       0                                 +6.24%  -77.41万   1008万   
    4   600893       0                                 +6.07%   -1138万   2.09亿   
    5   300696       0                                 +5.00%  -74.07万   1606万   
    6   300581       0                                 +4.30%    -132万    207万   
    7   600038       0                                 +3.21%     213万   4097万   
    8   600862       0      renqilong2                 +3.14%    -502万   5080万   
    """
    # dxw_api_zhangfu_gegu()